import datetime

from  selenium import  webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions  as EC

import time

chrome_driver = ChromeDriverManager().install()
Options = webdriver.ChromeOptions()
Options.page_load_strategy = "eager"
Options.add_argument("-headless")

# 可以设置一下标签句柄的加载策略
driver = webdriver.Chrome(service=Service(chrome_driver), options=Options)

#设置显示等待时间
driver_wait = WebDriverWait(driver, 3)

wait = driver.implicitly_wait(7) #wait 7秒
driver.get("https://www.baidu.com")
driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(4)").click()
#切换新标签
curent_ptr = driver.current_window_handle
all_ptr = driver.window_handles
for i in all_ptr:
    if i != curent_ptr:
        driver.switch_to.window(i)

ret = driver_wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                          "#tiebaCustomPassLogin > div.tieba-login-wrapper > span")))
ret.click()
driver.find_element(By.CSS_SELECTOR, "#wd1").send_keys("战地一")
driver.find_element(By.CSS_SELECTOR,
                    "#tb_header_search_form > span.search_btn_wrap.search_btn_enter_ba_wrap > a").click()
it = driver_wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                        "#tiebaCustomPassLogin > div.tieba-login-wrapper > span")))
it.click()
if driver.find_element(By.CSS_SELECTOR, "#tab_picture").get_attribute("frs-page") == "main":
    str = datetime.datetime.now().strftime("%H%M%S" + ".png")
    driver.save_screenshot(str)
    print("图片元素 正常")

driver.quit()

















